#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>
#include <QGridLayout>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    // 先设置六个按钮
    QPushButton* button1 = new QPushButton("按钮");
    QPushButton* button2 = new QPushButton("按钮");
    QPushButton* button3 = new QPushButton("按钮");
    QPushButton* button4 = new QPushButton("按钮");
    QPushButton* button5 = new QPushButton("按钮");
    QPushButton* button6 = new QPushButton("按钮");
    // 设置按钮可以拉伸
    button1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button4->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button5->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button6->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    // 创建网格布局管理器
    QGridLayout* grid = new QGridLayout(this);
    // 添加按钮到网格中 3x2
    grid->addWidget(button1, 0,0);
    grid->addWidget(button2, 0,1);
    grid->addWidget(button3, 1,0);
    grid->addWidget(button4, 1,1);
    grid->addWidget(button5, 2,0);
    grid->addWidget(button6, 2,1);
    // 设置垂直拉伸系数
    grid->setRowStretch(0, 1);
    grid->setRowStretch(1, 1);
    grid->setRowStretch(2, 2);
}

Widget::~Widget()
{
    delete ui;
}

